/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package objects;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUquadric;

/**
 *
 * @author rob
 */
public class Primitive {

    private String primitiveActivated;
    private GLUquadric quadric;
    private GLU glu;

    public Primitive()
    {
        primitiveActivated = "";
        glu = new GLU();    // this is okay as a global, but only use in callbacks
    }

    public void setPrimitive(String prim)
    {
        primitiveActivated = prim;
    }

    public void drawPrimitive(GL gl, GLU glu, GLUquadric quadric)
    {
        if(primitiveActivated.equalsIgnoreCase("cube"))
        {
            drawCube(gl);
        }
        else if(primitiveActivated.equalsIgnoreCase("plane"))
        {
            drawPlane(gl);
        }
        else if(primitiveActivated.equalsIgnoreCase("tube"))
        {
            drawTube(gl);
        }
        else if(primitiveActivated.equalsIgnoreCase("sphere"))
        {
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            drawSphere(glu, quadric);
        }
        else if(primitiveActivated.equalsIgnoreCase("cone"))
        {
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            drawCone(glu, quadric);
        }

        else if(primitiveActivated.equalsIgnoreCase("cubeonplane"))
        {
            drawCubeOnPlane(gl);
        }
    }

    public void drawPlane(GL gl)
    {
        
        gl.glBegin(gl.GL_QUADS);
            //Plane
            gl.glColor3f(0.2f, 0.4f, 0.3f);

            for(float j = -5.0f; j < 5.0f; j+=1.0f)
            {
                for(float i = 5.0f; i > -5.0f; i-=1.0f)
                {
                    gl.glVertex3f(i, -1.0f,      j);
                    gl.glVertex3f(i-1.0f, -1.0f, j);
                    gl.glVertex3f(i-1.0f, -1.0f, j+1.0f);
                    gl.glVertex3f(i, -1.0f,      j+1.0f);
                }
            }
            
        gl.glEnd();
    }

    public void drawCubeOnPlane(GL gl)
    {
        drawCube(gl);
        drawPlane(gl);

    }

    public void drawCube(GL gl)
    {

        gl.glBegin(gl.GL_QUADS);
            //Top face
            gl.glColor3f(1.0f, 0.5f, 0.0f);
            gl.glVertex3f(1.0f, 1.0f, -1.0f);
            gl.glVertex3f(-1.0f, 1.0f, -1.0f);
            gl.glVertex3f(-1.0f, 1.0f, 1.0f);
            gl.glVertex3f(1.0f, 1.0f, 1.0f);

            //Bottom
            gl.glColor3f(0.0f, 0.5f, 0.0f);
            gl.glVertex3f(1.0f, -1.0f, -1.0f);
            gl.glVertex3f(-1.0f, -1.0f, -1.0f);
            gl.glVertex3f(-1.0f, -1.0f, 1.0f);
            gl.glVertex3f(1.0f, -1.0f, 1.0f);

            //Front
            gl.glColor3f(0.5f, 0.5f, 0.0f);
            gl.glVertex3f(1.0f, 1.0f, 1.0f);
            gl.glVertex3f(-1.0f, 1.0f, 1.0f);
            gl.glVertex3f(-1.0f, -1.0f, 1.0f);
            gl.glVertex3f(1.0f, -1.0f, 1.0f);

            //Back
            gl.glColor3f(1.0f, 0.0f, 1.0f);
            gl.glVertex3f(1.0f, 1.0f, -1.0f);
            gl.glVertex3f(-1.0f, 1.0f, -1.0f);
            gl.glVertex3f(-1.0f, -1.0f, -1.0f);
            gl.glVertex3f(1.0f, -1.0f, -1.0f);

            //Left
            gl.glColor3f(0.3f, 0.5f, 1.0f);
            gl.glVertex3f(-1.0f, 1.0f, 1.0f);
            gl.glVertex3f(-1.0f, 1.0f, -1.0f);
            gl.glVertex3f(-1.0f, -1.0f, -1.0f);
            gl.glVertex3f(-1.0f, -1.0f, 1.0f);

            //Right
            gl.glColor3f(0.0f, 0.1f, 1.0f);
            gl.glVertex3f(1.0f, 1.0f, 1.0f);
            gl.glVertex3f(1.0f, 1.0f, -1.0f);
            gl.glVertex3f(1.0f, -1.0f, -1.0f);
            gl.glVertex3f(1.0f, -1.0f, 1.0f);

         gl.glEnd();
    }
    
    public void drawTube(GL gl)
    {
        gl.glBegin(gl.GL_QUADS);
            //Top face
            gl.glColor3f(1.0f, 0.5f, 0.0f);
            gl.glVertex3f(-10.5488f, 0.0000f, -30.1585f);
            gl.glVertex3f(-10.5488f, 10.7655f, -30.1585f);
            gl.glVertex3f(7.5324f, 10.7655f, -26.9703f);
            gl.glVertex3f(7.5324f, 0.0000f, -26.9703f);
            gl.glVertex3f( -10.5488f, 21.5310f, -30.1585f);
            gl.glVertex3f(7.5324f, 21.5310f, -26.9703f);
            gl.glVertex3f(-10.5488f, 32.2965f, -30.1585f);
            gl.glVertex3f(7.5324f, 32.2965f, -26.9703f);
            gl.glVertex3f(-10.5488f, 43.0620f, -30.1585f);
         gl.glEnd();
    }

    public void drawSphere(GLU glu, GLUquadric quadric)
    {
        this.glu = glu;
        this.quadric = quadric;
       
        glu.gluSphere(quadric, 1.3f, 10, 10);
    }

    public void drawCone(GLU glu, GLUquadric quadric)
    {
        this.glu = glu;
        this.quadric = quadric;

        glu.gluCylinder(quadric, 1.5f, 0.0f,  3.0, 20, 10);
    }



}
